﻿// Dùng tại frmTongHopDT.sbtnHoaHong2_Click()

using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;
using prjKQXS_DLL.Doanh_thu;

namespace prjKQXS_Report
{
    public partial class rptMauHH01y : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();

        public rptMauHH01y()
        {
            InitializeComponent();
        }

        private void rptMauHH01y_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            fillHeader(dsReports1);

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            clsDoanhThu cls = new clsDoanhThu();
            cls.OnAction += new clsDoanhThu.OnActionHandler(cls_OnAction);
            cls.OnError += new clsDoanhThu.OnErrorHandler(cls_OnError);

            string slblSumHT2 = null;
            dsReports1.Tables.Remove("rptHoaHong");
            dsReports1.Tables.Add(cls.loadData_rptMauHH01y((char)pMaDV.Value, (DateTime)pTuNgay.Value, (DateTime)pDenNgay.Value, ref slblSumHT2, (bool)pThiTran.Value));
            this.DataSource = dsReports1;
            this.DataMember = "rptHoaHong";
        }

        private void rptMauHH01y_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }

        #region cls event --------------------------------------------
        private void cls_OnAction(SqlException e)
        {
            cls_OnError(e);
        }

        private void cls_OnError(Exception e)
        {
            XtraMessageBox.Show("Một lỗi chưa được xử lý đã xảy ra. Chi tiết lỗi: \n" + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.ClosePreview();
        }
        #endregion ---------------------------------------------------

        private void fillHeader(dsReports dsReports1)
        {
            if (pMaDV.Value == "%") lblMau.Text = "Mẫu HH-01A";
            else
            {
                lblMau.Text = "Mẫu HH-01B";
                GroupFooter1.Visible = false;
            }

            dsReports1.rptHoaHongByDaiLy1.Rows.Clear();
            if ((char)pMaDV.Value != '%')
            {
                if (pTenDV.Value != null && pTenDV.Value.ToString().Length > 0) lblDonVi.Text = "PHÒNG XSKT " + pTenDV.Value.ToString().ToUpper();
                lblPhong.Text = "PHÒNG XSKT " + pTenDV.Value.ToString().ToUpper();
            }
            else
            {
                lblPhong.Text = " KẾ TOÁN ";
                xrLabel8.Visible = false;
            }

            if ((DateTime)pTuNgay.Value == (DateTime)pDenNgay.Value)
                lblNgayThang.Text = "Ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy");
            else
                lblNgayThang.Text = "Từ ngày " + ((DateTime)pTuNgay.Value).ToString("dd/MM/yyyy") + " đến ngày " + ((DateTime)pDenNgay.Value).ToString("dd/MM/yyyy");
        }

        int stt = 0;
        private void GroupHeader1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            double d = GetCurrentColumnValue<double>("HoaHong");
            if (d==0) e.Cancel = true;
            else xrtcSTT.Text = (++stt).ToString();
        }
    }
}
